#认证的前提是需要登陆成功 1121
import requests
import pytest

from api import log
from api.api_approve_trust import ApiApproveTrust
from api.api_register_login import ApiRegisterLogin
from tools import common_assert, html_parse


class TestApproveTrust:
    @pytest.fixture(autouse=True)
    #初始化
    def setup(self):
        #获取session
        self.session = requests.session()
        #获取ApiApproveTrust实例
        self.api=ApiApproveTrust(self.session)
        #获取RegisterLogin实例对象调用登录
        r = ApiRegisterLogin(self.session).api_login()
    #结束
    def teardown(self):
        #关闭session
        self.session.close()
    #认证接口测试
    def test01_approve(self,expect_json="提交成功"):
        try:
            result =self.api.api_approve("天天","110102195508265891")
            log.info("认证接口的测试结果为：{}".format(result.text))
            #断言
            common_assert(result,json_desc=expect_json)
            log.info("认证接口断言通过")
        except Exception as e:
            log.error(e)
            raise
    #认证查询接口测试
    def test02_approve_status(self,expect_text="1121"):
        try:
            result = self.api.api_approve_status()
            log.info("认证接口的测试结果为：{}".format(result.text))
            # 断言
            common_assert(result,text=expect_text)
            log.info("认证查询接口断言通过")
        except Exception as e:
            log.error(e)
            raise
            #开户接口测试
    def test03_trust(self,expect_text="form",expect_text2="OK"):
        try:
            result = self.api.api_trust()
            print(result.json())
            log.info("后台开户接口的测试结果为：{}".format(result.json()))
            # 断言
            common_assert(result, text=expect_text)
            log.info("后台开户接口断言通过")
            #三方开户如何实现
            data = html_parse(result)
            result =self.session.post(url=data[0], data=data[1])
            log.info("三方开户接口执行结果为{}" .format(result.text))
            common_assert(result,text=expect_text2)
            log.info("三方接口断言通过")
        except Exception as e:
            log.error(e)
            raise